import Pagination from "@/components/pagination";
import {
  Message
} from "element-ui";
export default {
  components: {
    Pagination
  },
  data() {
    return {
      eduKindName:'', // 学历中文
      editType:'',
      deletIds: [], // 批量删除id数组
      teaName: '', // 导师名字
      teaSelectList:[],
      loading:false,
      teacherRadio: '', // 判断导师类型
      checkChoose: false, // 选择内外部导师弹窗
      total: 10,
      tipText: "",
      tipTexts: "",
      status: "",
      searchLoading: false,
      checkApply: false, // 内部课程弹窗控制
      checkApplys: false, // 外部课程弹窗控制
      name: "",
      teaType: '',
      deptName: "",
      date: "",
      tableData: [],
      teaIsGood: null,
      outTeaRule: {
        username: [{
          required: true,
          message: "请输入讲师名称",
          trigger: "blur"
        }],
        honorTitle: [{
          required: true,
          message: "请输入荣誉称号",
          trigger: "blur"
        }],
        school: [{
          required: true,
          message: "请输入毕业院校",
          trigger: "blur"
        }],
        jobTitle: [{
          required: true,
          message: "请输入职称",
          trigger: "blur"
        }],
        achievement: [{
          required: true,
          message: "请输入讲师成就",
          trigger: "blur"
        }],
        eduKind: [{
          required: true,
          message: "请输入讲师成就",
          trigger: "change"
        }],
      },
      inTeacherRules: {
        teaName: [{
          required: true,
          message: "请输入讲师名称",
          trigger: "input"
        }],
        
        honorTitle: [{
          required: true,
          message: "请输入荣誉称号",
          trigger: "blur"
        }],
        school: [{
          required: true,
          message: "请输入毕业院校",
          trigger: "blur"
        }],
        achievement: [{
          required: true,
          message: "请输入讲师成就",
          trigger: "blur"
        }]
      },
      inTeacherData: { // 内部导师对象
        "userId":'',
        "honorTitle":"",
        "achievement":"",
      },
      outTeacherData: { // 外部导师对象
        "username":"",
        "honorTitle":"",
        "achievement":"",
        "jobTitle":"",
        "eduKind":'',
        "school":"",
        "isGood": false
      },
      eduKindobj: [{
        value: '0',
        label: '大专'
      }, {
        value: '1',
        label: '本科'
      }, {
        value: '2',
        label: '硕士'
      }, {
        value: '3',
        label: '博士'
      }, {
        value: '4',
        label: '其它'
      }]
    };
  },
  mounted() {
    this.searchList(1, 10);
  },
  methods: {
    eduKindSelect(e){
      this.outTeacherData.eduKind = e;
    },
    querySearch(queryString) {
      let params = {
        userName: queryString
      }
      let that = this;
      that.$get('qryUserBySelect', params).then(res => {
        if (res.success) {
          this.teaSelectList = res.data;
          this.loading = false;
        }else{
          this.loading = false;
        }
      })
    },
    handleSelect(val) {
      // this.teaName = val.username;
      this.inTeacherData.userId = val;
    },
    pageNum(prop) {
      this.searchList(prop, 10);
    },
    pageSizeNum(prop) {
      this.searchList(1, prop);
    },
    initData(row, column) {
      return row.teaType == 0 ? "内部讲师" : row.teaType == 1 ? "外部讲师" : "暂无";
    },
    handleClose0(done) {
      this.$nextTick(()=>{
        this.$refs.insertInTea.clearValidate()
      })
      done();
    },
    handleClose1(done) {
      this.$nextTick(()=>{
        this.$refs.insertOutTea.clearValidate()
      })
      done();
    },
    closeDiloag(showStatus,form){
      this[showStatus] = false;
      this.$nextTick(()=>{
        //等弹窗里的form表单的dom渲染完在执行this.$refs.staffForm.resetFields()，去除验证
        this.$refs[form].clearValidate();
      });
    },
    addTeacher() {
      this.status = "add";
      this.tipText = "新增讲师";
      this.tipTexts = "录入讲师信息";
      this.checkChoose = true;
      this.outTeacherData = {
        username: "",
        honorTitle: "",
        achievement: "",
        jobTitle: "",
        eduKind: '',
        teaType: '',
        school: ""
      };
      this.inTeacherData = {
        "userId":'',
        "honorTitle":"",
        "achievement":"",
      };
      this.eduKindName = '';
      this.teaName = '';
      this.editType = '';
    },
    goDetailModal() {
      if (!this.teacherRadio) {
        Message({
          message: "请选择课程类型",
          type: "warning",
          duration: 2000
        });
        return;
      }
      if (this.teacherRadio == 2) {
        this.checkApplys = true;
        this.tipText = "新增外部讲师";
        this.tipTexts = "录入讲师信息";
        this.checkChoose = false;
      } else {
        this.checkApply = true;
        this.tipText = "新增内部讲师";
        this.tipTexts = "录入讲师信息";
        this.checkChoose = false;
      }
    },
    addOutTea() {
      let editType = this.editType;
      this.confirm('insertOutTea', 'insertOutTea', 'out' ,editType);
    },
    addInTea() {
      let editType = this.editType;
      this.confirm('insertInTea', 'insertInnerTea', 'in', editType );
    },
    teaEdit(type, id , updateId) {
      let params = {
        id
      };
      this.editType = 1;
      this.$get('qryTeaInfoById', params).then(res => {
        if (res.success) {
          if (type == 0) {
            this.checkApply = true;
            this.tipText = "编辑内部讲师";
            this.tipTexts = "录入讲师信息";
            this.checkChoose = false;
            let {honorTitle,achievement, isGood, id} = res.data;
            this.inTeacherData = { // 内部导师对象
              id:updateId,
              honorTitle,
              achievement,
              isGood,
              userId: id
            },
            this.teaName = res.data.username;
          } else {
            this.checkApplys = true;
            this.tipText = "编辑外部讲师";
            this.tipTexts = "录入讲师信息";
            this.checkChoose = false;
            this.outTeacherData = res.data;
            let {username,honorTitle,achievement,jobTitle,eduKind,school,id} = res.data;
            if(eduKind == 0){
              this.eduKindName = '大专'
            }else if(eduKind == 1){
              this.eduKindName = '本科'
            }else if(eduKind == 2){
              this.eduKindName = '硕士'
            }else if(eduKind == 3){
              this.eduKindName = '博士'
            }else{
              this.eduKindName = '其它'
            }
            this.outTeacherData = { // 外部导师对象
              username,
              honorTitle,
              achievement,
              jobTitle,
              eduKind,
              school,
              id:updateId
            }
          }
        }
      })

    },
    handleSelectionChange(e) {
      let that = this;
      let idArr = [];
      e.map(function (item) {
        idArr.push(item.id);
      });
      that.deletIds = idArr;
    },
    deleteOne(id) {
      let params = {
        id
      };
      this.$confirm("删除后不可恢复，确定删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$get("deleteTeaById", params, true).then(res => {
          if (res.success) {
            Message({
              message: "操作成功",
              type: "success",
              duration: 2000
            });
            this.searchList(1, 10);
          }
        });
      })
    },
    deleteClass() {
      let ids = this.deletIds;
      if(ids.length == 0){
        Message({
          message: "请选择要删除项",
          type: "warning",
          duration: 2000
        });
        return;
      }
      this.$confirm("删除后不可恢复，确定删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(()=>{
        this.$post("deleteTeaByIds", ids, true).then(res => {
          if (res.success) {
            Message({
              message: "操作成功",
              type: "success",
              duration: 2000
            });
  
            this.searchList(1, 10);
          }
        });
      })
    },
    confirm(formName, url, type ,editType) {
      let that = this;
      let params;
     
      if(type == 'in'){
        params = that.inTeacherData;
        if(editType == 1){
          url = 'updateInnerTea';
          that.setGoodTea(that.inTeacherData.userId, that.inTeacherData.isGood);
        }
        if(!that.teaName){
          Message({
            message: "请填写讲师名称",
            type: "warning",
            duration: 2000
          });
          return
        }
      }else{
        if(editType == 1){
          url = 'updateOutTea';
        }
        params = that.outTeacherData;
      }
      that.$refs[formName].validate(valid => {
        if (valid) {
          that.$post(url, params).then(res => {
            if (res.success) {
              Message({
                message: "操作成功",
                type: "success",
                duration: 2000
              });
              if (type == 'out') {
                that.checkApplys = false;
              } else {
                that.checkApply = false;
              }
              that.searchList(1, 10);
            }
          });
        } else {
          Message({
            message: "请完善必填项",
            type: "warning",
            duration: 2000
          });
          return;
        }
      })
    },
    searchList(page, pageSize) {
      let {
        name,
        teaType,
        deptName,
        teaIsGood
      } = this;
      let params = {
        name,
        teaType,
        deptName,
        isGood: teaIsGood,
        page,
        pageSize
      };
      this.$post("qryTeaPCResult", params).then(res => {
        if (res.success) {
          this.tableData = res.data.teaPCBacks;
          this.total = res.data.totals;
        }
      });
    },
    setGoodTea(id, isGood){
      this.$get("setGoodTea", {
        id: id,
        isGood: isGood
      }).then(res => {
        if (res.success) {
        }
      });
    }
  }
};
